<template>
  <div style="height: 100%">
    <el-container style="height: 100%">
      <el-aside :width="!menuCollapse ? '200px' : '64px'">
        <!-- 菜单栏 -->
        <el-menu
          :default-active="activeMenu"
          router
          :collapse="menuCollapse"
          class="el-menu-vertical-demo"
          background-color="#304156"
          text-color="#9fcbd9"
          active-text-color="#409eff"
        >
          <el-menu-item index="/welcome">首页</el-menu-item>
          <!-- 一级子菜单 -->
          <el-submenu :index="formatPath(null, item.name)" v-for="item in firstMenu" :key="item.id">
            <!-- 一级子菜单标题 -->
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.title}}</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item class="menu-second" :index="formatPath(item.name, subItem.name)" v-for="subItem in secondMenu(item.id)" :key="subItem.id">{{subItem.title}}</el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <!-- 一级子菜单标题 -->
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>导航二</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item index="1-4-1">选项1</el-menu-item>
            <el-menu-item index="1-4-1">选项2</el-menu-item>
            <el-menu-item index="1-4-1">选项3</el-menu-item>
          </el-submenu>
          <el-submenu index="3">
            <!-- 一级子菜单标题 -->
            <template slot="title">
              <i class="el-icon-document"></i>
              <span>导航三</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item index="1-4-1">选项1</el-menu-item>
            <el-menu-item index="1-4-1">选项2</el-menu-item>
            <el-menu-item index="1-4-1">选项3</el-menu-item>
          </el-submenu>
          <el-submenu index="4">
            <!-- 一级子菜单标题 -->
            <template slot="title">
              <i class="el-icon-setting"></i>
              <span>导航四</span>
            </template>
            <!-- 二级子菜单 -->
            <el-menu-item index="1-4-1">选项1</el-menu-item>
            <el-menu-item index="1-4-1">选项2</el-menu-item>
            <el-menu-item index="1-4-1">选项3</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <el-button
            :icon="!menuCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
            @click="menuCollapse = !menuCollapse"
          ></el-button>
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              欢迎：
              <el-avatar
                :src="loginUser.icon"
              ></el-avatar>
              {{loginUser.username}}
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item command="change">修改密码</el-dropdown-item>
              <el-dropdown-item command="quit">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-header>
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { api_logout, api_info } from '../api/admin.js'

export default {
  data() {
    return {
      menuCollapse: false,
      activeMenu: '',
      loginUser: {
        username: '',
        icon: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'
      },
      menus: []
    }
  },
  filter: {

  },
  computed: {
    firstMenu() {
      return this.menus.filter(item => item.parentId === 0)
    },
    secondMenu() {
      return parentId => this.menus.filter(item => item.parentId === parentId)
    }
  },
  mounted() {
    this.activeMenu = window.sessionStorage.getItem('activeMenu')
    if (!this.activeMenu) {
      this.activeMenu = '/welcome'
    }
    this.getInfo()
  },
  watch: {
    $route(to) {
      // console.log(to);
      this.selectMenu(to.path)
    }
  },
  methods: {
    async getInfo() {
      const {data: result} = await api_info()
      console.log(result)
      if(result) {
        this.loginUser.icon = result.data.icon
        this.loginUser.username = result.data.username
        this.menus = result.data.menus
      }
    },
    formatPath(folder, name) {
      name = '/' + name
      if(folder) {
        name = '/' + folder + name
      }
      return name
    },

    selectMenu(index) {
      console.log(index)
      this.activeMenu = index
      window.sessionStorage.setItem('activeMenu', index)
    },
    handleCommand(command) {
      console.log(command)
      if (command === 'quit') {
        this.handleLogout()
      }
    },
    async handleLogout() {
      const {data: result} = await api_logout()
      // console.log(result)
      if (result.code !== 200) {
        //退出失败
        this.$message.error(result.message)
        return
      }
      //退出成功
      //清除session
      window.sessionStorage.clear()
      //跳转登录页面
      await this.$router.replace('/login')
    }
  }
}
</script>

<style>
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.menu-second {
  background-color: #1f2d3d!important;
}
.menu-second:hover {
  background-color: #001528!important;
}

.el-menu {
  border: none;
}

.el-menu > .el-menu-item,
.el-submenu > .el-submenu__title {
  text-align: left;
}

.el-header,
.el-footer {
  background-color: #ffffff;
  text-align: center;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: #d3dce6 solid 1px;
}

.el-avatar {
  position: relative;
  top: 12px;
}

.el-aside {
  background-color: #304156;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: left;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}
</style>
